package com.qdu.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.qdu.entity.Users;
import com.qdu.service.HousetypeService;
import com.qdu.service.UsersService;

@Controller
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UsersService usersService;
    
    @Autowired
    private HousetypeService houseService;
    
    @PostMapping("/login")
    public String login(String Uid,String Upwd,Model model,HttpSession session)
    {
    	Users user = usersService.getUserById(Uid);
    	if(user.getUatt().equals("1")) {
    	if(user.getUpwd().equals(Upwd))
    	{
    		session.setAttribute("sessionuser", user);
    		session.setAttribute("houselist",houseService.getallHouseList());
    	    return "index";
    	}
    	else {
    		return "Upwderror";
    	}}
    	else {
    		return "UserPass";
    	}
    }
    
    @GetMapping("/logout")
    public String logout(HttpSession session) {
    	session.invalidate();
    	return "Login";
    }

    
    @GetMapping("/user_Info/{Uid}")
    public String FindUserbyuid(@PathVariable String Uid,Model model)
    {
    	model.addAttribute("user",usersService.getUserById(Uid));
    	return "userInfo";
    }
    
    @PostMapping("/edit_user")
    public String edituser(Users user,Model model) {
        usersService.updateUsers(user);
        model.addAttribute("user",user);
        return "index";
    }
    
    @PostMapping("/edit_user2")
    public String edituser2(Users user,Model model) {
        usersService.updateUsers(user);
        model.addAttribute("user",user);
        return "Adminindex";
    }
    

    @PostMapping("/register")
    public String register(Users user,Model model) {
        usersService.addUsers(user);
    	model.addAttribute("Uid",user.getUid());
        return "Login";
    }
    
    @GetMapping("/modifypwd_page/{Uid}")
    public String tomodifypwd(@PathVariable String Uid,Model model){
    	model.addAttribute("user",usersService.getUserById(Uid));
    	return "modifypwd";
    }
    
    @PostMapping("/modifypwd")
    public String modifypwd(String Uid,String oldUpwd,String newUpwd) {
    	Users user = usersService.getUserById(Uid);
    	if(user.getUpwd().equals(oldUpwd)) {
    		usersService.modifypwd(Uid,newUpwd);
    		return "Login";
    	}else {
    		return "oldpwderror";
    	}
    	
    	
    }
    
    @GetMapping("/tochongzhipage")
    public String tochongzhipage() {
    	
    	return "chongzhipage";
    }
    
    @PostMapping("/chongzhi")
    public String chongzhi(String Uid,String Uaccount,HttpSession session) {
    	Users user=usersService.getUserById(Uid);
    	String oldUaccount=user.getUaccount();
    	Integer oldUaccount2=Integer.valueOf(oldUaccount);
    	Integer Uaccount2=Integer.valueOf(Uaccount);
    	Integer newUaccount = oldUaccount2+Uaccount2;
    	String newUaccount2=String.valueOf(newUaccount);
    	user.setUaccount(newUaccount2);
    	usersService.updateUsers(user);
    	session.setAttribute("sessionuser", user);
    	return "index";
    }
    
    
}
